package com.genITeam.ria.actions;

import java.util.ArrayList;

import javax.servlet.http.HttpServletRequest;
import javax.servlet.http.HttpServletResponse;

import org.apache.log4j.BasicConfigurator;
import org.apache.log4j.Logger;

import com.genITeam.ria.bl.UserRequestBL;
import com.genITeam.ria.vo.*;
import com.genITeam.ria.exception.NGFException;
import com.genITeam.ria.vo.ModeratorRequest;

public class RightsManagementAction extends AbstractAction {
	public RightsManagementAction() {
		BasicConfigurator.configure();
	}

	Logger logger = Logger.getLogger(MemberAction.class);

	public void handleUserRequestSubmission(HttpServletRequest request,
			HttpServletResponse response) throws NGFException {
		UserRequestBL userRequestBL = new UserRequestBL();
		ModeratorRequest moderatorRequestVo = new ModeratorRequest();
		Posts p = new Posts();
		Member m = new Member();
		
		try {
			//String id = request.getParameter("id");
			String memberid = request.getParameter("memberid");
//			String reqType = request.getParameter("request");
			String desc = request.getParameter("description");
			String postid = request.getParameter("postid");
			Character status = new Character('f');
			m.setId(Integer.parseInt(memberid));
			p.setPostId(Integer.parseInt(postid));
			moderatorRequestVo.setMember(m);
			moderatorRequestVo.setPosts(p);
			moderatorRequestVo.setDescription(desc);
			moderatorRequestVo.setStatus(status);
			userRequestBL.submitUserRequest(moderatorRequestVo);
			
			
			
		} catch (Exception e) {
			logger.error("Exception=" + e.toString());

			this.initAction(request, response);
			this.writeResponse("<error>" + e.getMessage() + "</error>");

		}
	}

	public void handleAdminRequestView(HttpServletRequest request,
			HttpServletResponse response) throws NGFException {

		try {
			System.out.println("fakhri\n\n\n\n in action");
			UserRequestBL userRequestBL = new UserRequestBL();
			ModeratorRequest moderatorRequestVo = new ModeratorRequest();
			ArrayList list = userRequestBL.getUserRequest();
			String xml = userRequestBL.generateRequestsXml(list);
			this.initAction(request, response);
			this.writeResponse(xml);
			//this.writeResponse("<message>" + "success" + "</message>");
			
		} catch (Exception e) {
			logger.error("Exception=" + e.toString());

			this.initAction(request, response);
			this.writeResponse("<error>" + e.getMessage() + "</error>");

		}
	}
	
	public void handleApproveRequestSubmission(HttpServletRequest request,
			HttpServletResponse response) throws NGFException {
		UserRequestBL userRequestBL = new UserRequestBL();
		Posts p = new Posts();
		Member m = new Member();
		
		try {
			String id = request.getParameter("reqid");
			ModeratorRequest moderatorRequestVo = userRequestBL.getRecord(Integer.parseInt(id));
				
			//String memberid = request.getParameter("requsername");
			//String reqType = request.getParameter("request");
//			String desc = request.getParameter("reqdesc");
//			String postid = request.getParameter("reqforumtitle");
//			Character status = new Character('y');
//			m.setId(Integer.parseInt(memberid));
//			p.setPostId(Integer.parseInt(postid));
//			moderatorRequestVo.setId(Integer.parseInt(id));
//			moderatorRequestVo.setMember(m);
//			moderatorRequestVo.setPosts(p);
//			moderatorRequestVo.setDescription(desc);
//			moderatorRequestVo.setStatus(status);
			moderatorRequestVo.setStatus(new Character('y'));
			userRequestBL.approveRequest(moderatorRequestVo);
			
			
			
		} catch (Exception e) {
			logger.error("Exception=" + e.toString());

			this.initAction(request, response);
			this.writeResponse("<error>" + e.getMessage() + "</error>");

		}
	}
	
	
	public void handleRejectRequestSubmission(HttpServletRequest request,
			HttpServletResponse response) throws NGFException {
		UserRequestBL userRequestBL = new UserRequestBL();
		Posts p = new Posts();
		Member m = new Member();
		
		try {
			String id = request.getParameter("reqid");
			ModeratorRequest moderatorRequestVo = userRequestBL.getRecord(Integer.parseInt(id));
				
			//String memberid = request.getParameter("requsername");
			//String reqType = request.getParameter("request");
//			String desc = request.getParameter("reqdesc");
//			String postid = request.getParameter("reqforumtitle");
//			Character status = new Character('y');
//			m.setId(Integer.parseInt(memberid));
//			p.setPostId(Integer.parseInt(postid));
//			moderatorRequestVo.setId(Integer.parseInt(id));
//			moderatorRequestVo.setMember(m);
//			moderatorRequestVo.setPosts(p);
//			moderatorRequestVo.setDescription(desc);
//			moderatorRequestVo.setStatus(status);
			moderatorRequestVo.setStatus(new Character('r'));
			userRequestBL.approveRequest(moderatorRequestVo);
			
			
			
		} catch (Exception e) {
			logger.error("Exception=" + e.toString());

			this.initAction(request, response);
			this.writeResponse("<error>" + e.getMessage() + "</error>");

		}
	}

}
